﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace zEngine
{
    public class DrawPrimatives
    {
        // Draw an axis coord in the input position
        public static void DrawAxis(Vector3 pos, int length, Color col, Cameras.Camera cam, GraphicsDevice device)
        {
            VertexDeclaration thisVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);

            #region Prepare vertices
            List<VertexPositionColor> vpc = new List<VertexPositionColor>();

            vpc.Add(new VertexPositionColor(pos, col));
            vpc.Add(new VertexPositionColor(new Vector3(pos.X + length, pos.Y, pos.Z), col));

            vpc.Add(new VertexPositionColor(pos, col));
            vpc.Add(new VertexPositionColor(new Vector3(pos.X, pos.Y + length, pos.Z), col));

            vpc.Add(new VertexPositionColor(pos, col));
            vpc.Add(new VertexPositionColor(new Vector3(pos.X, pos.Y, pos.Z + length), col));
            #endregion

            device.RenderState.CullMode = CullMode.None;
            BasicEffect bEff = new BasicEffect(device, null);
            bEff.World = Matrix.Identity;
            bEff.View = cam.ViewMatrix;
            bEff.Projection = cam.ProjectionMatrix;
            bEff.VertexColorEnabled = true;

            bEff.Begin();
            foreach (EffectPass pass in bEff.CurrentTechnique.Passes)
            {
                pass.Begin();
                device.VertexDeclaration = thisVertexDeclaration;
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vpc.ToArray(), 0, 3);
                pass.End();
            }
            bEff.End();
        }

        public static void DrawBoundingBox(BoundingBox box, Color col, Cameras.Camera cam, GraphicsDevice device)
        {
            VertexDeclaration thisVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            Vector3 ip = box.Min;
            Vector3 xp = box.Max;
            List<VertexPositionColor> vpc = new List<VertexPositionColor>();

            #region Prepare vertices
            // Bottom rectangle
            vpc.Add(new VertexPositionColor(ip, col));
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, ip.Y, xp.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(ip.X, ip.Y, xp.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, xp.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, xp.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, ip.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, ip.Z), col));
            vpc.Add(new VertexPositionColor(ip, col));

            // Top rectangle
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, ip.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, xp.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, xp.Z), col));
            vpc.Add(new VertexPositionColor(xp, col));

            vpc.Add(new VertexPositionColor(xp, col));
            vpc.Add(new VertexPositionColor(new Vector3(xp.X, xp.Y, ip.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(xp.X, xp.Y, ip.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, ip.Z), col));

            // Connect top to bottom

            vpc.Add(new VertexPositionColor(ip, col));
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, ip.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(ip.X, ip.Y, xp.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(ip.X, xp.Y, xp.Z), col));

            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, xp.Z), col));
            vpc.Add(new VertexPositionColor(xp, col));

            vpc.Add(new VertexPositionColor(new Vector3(xp.X, ip.Y, ip.Z), col));
            vpc.Add(new VertexPositionColor(new Vector3(xp.X, xp.Y, ip.Z), col));
            #endregion

            device.RenderState.CullMode = CullMode.None;
            BasicEffect bEff = new BasicEffect(device, null);
            bEff.World = Matrix.Identity;
            bEff.View = cam.ViewMatrix;
            bEff.Projection = cam.ProjectionMatrix;
            bEff.VertexColorEnabled = true;

            bEff.Begin();
            foreach (EffectPass pass in bEff.CurrentTechnique.Passes)
            {
                pass.Begin();
                device.VertexDeclaration = thisVertexDeclaration;
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vpc.ToArray(), 0, 12);
                pass.End();
            }
            bEff.End();
        }
    }
}
